package com.dpiinc.TechPage.activity;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.dpiinc.TechPage.BaseActivity;
import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.UserManager;
import com.dpiinc.TechPage.adapter.GoodsListRecyclerViewAdapter;
import com.dpiinc.TechPage.api.BaseObserver;
import com.dpiinc.TechPage.api.RetrofitFactory;
import com.dpiinc.TechPage.api.RxSchedulers;
import com.dpiinc.TechPage.loader.GlideImageLoader;
import com.dpiinc.TechPage.model.BaseEntity;
import com.dpiinc.TechPage.model.jbean.GoodsInfo;
import com.dpiinc.TechPage.model.jbean.StoreList;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import io.reactivex.Observable;

/**
 * 商城界面
 */
public class StoreActivity extends BaseActivity implements OnBannerListener {

    Banner banner;
    private ArrayList<String> imagePathBanner;
    private ArrayList<String> imageLinkBanner;

    RecyclerView recyclerView;
    GoodsListRecyclerViewAdapter adapter;

    View layout_store;
    View layout_network;
    Button button_network;
    SwipeRefreshLayout swipe_refresh_layout;

    public static final String TAG = "StoreActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_store);
        super.onCreate(savedInstanceState);
        layout_store = findViewById(R.id.layout_store);
        layout_network = findViewById(R.id.layout_network);
        button_network = findViewById(R.id.button_network);
        button_network.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initView();
            }
        });
        swipe_refresh_layout = findViewById(R.id.swipe_refresh_layout);
        swipe_refresh_layout.setEnabled(false);
        initView();

    }

    @Override
    public void OnBannerClick(int position) {
        Intent intent = new Intent();
        intent.setData(Uri.parse(imageLinkBanner.get(position)));
        intent.setAction(Intent.ACTION_VIEW);
        startActivity(intent);
    }

    private void initView(){
        NetworkInfo mNetworkInfo = ((ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if(mNetworkInfo != null){
            if(mNetworkInfo.isAvailable()){
                layout_store.setVisibility(View.VISIBLE);
                layout_network.setVisibility(View.GONE);
            }else{
                layout_store.setVisibility(View.GONE);
                layout_network.setVisibility(View.VISIBLE);
                return;
            }
        }else{
            layout_store.setVisibility(View.GONE);
            layout_network.setVisibility(View.VISIBLE);
            return;
        }
        HashMap<String, String> requestMap = getBaseMap();
        Observable<BaseEntity<StoreList>> observable;
        if("en".equals(getSharedPreferences("app_language",Context.MODE_PRIVATE).getString("lauType",Locale.getDefault().getLanguage()))){
            observable = RetrofitFactory.getInstance().getAbroadGoodsList(requestMap);
        }else{
            observable = RetrofitFactory.getInstance().getGoodsList(requestMap);
        }
        BaseObserver mBaseObserver = new BaseObserver<StoreList>(this) {
            @Override
            protected void onHandleError(int code) {
                super.onHandleError(code);
                Log.d(TAG, "getStoreList onHandleError code = " + code);

            }

            @Override
            protected void onHandleSuccess(StoreList baseBean) {
                Log.d(TAG, "getStoreList onHandleSuccess NoteLabelGetBean = " + baseBean);
                View header = LayoutInflater.from(StoreActivity.this).inflate(R.layout.header_banner, null);
                if (baseBean != null && baseBean.getBannerList() != null && baseBean.getBannerList().size() > 0) {
                    List<GoodsInfo> bannerList = baseBean.getBannerList();
                    Collections.sort(bannerList, new Comparator<GoodsInfo>() {
                        @Override
                        public int compare(GoodsInfo goodsInfo, GoodsInfo t1) {
                            if(goodsInfo.getWeight() > t1.getWeight())
                                return 1;
                            return -1;
                        }
                    });


                    imagePathBanner = new ArrayList<>();
                    imageLinkBanner = new ArrayList<>();
                    for (GoodsInfo li : bannerList) {
                        imagePathBanner.add(li.getImg());
                        imageLinkBanner.add(li.getLink());
                    }
                    banner = header.findViewById(R.id.banner);
                    //设置banner样式
                    banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                    //设置图片加载器
                    banner.setImageLoader(new GlideImageLoader());
                    //设置图片集合
                    banner.setImages(imagePathBanner);
                    //设置轮播时间
                    banner.setDelayTime(3000);
                    //设置监听事件
                    banner.setOnBannerListener(StoreActivity.this);
                    //banner设置方法全部调用完毕时最后调用
                    banner.start();
                }
                if (baseBean != null && baseBean.getGoodsList() != null && baseBean.getGoodsList().size() > 0) {
                    List<GoodsInfo> goodList = baseBean.getGoodsList();
                    Collections.sort(goodList, new Comparator<GoodsInfo>() {
                        @Override
                        public int compare(GoodsInfo goodsInfo, GoodsInfo t1) {
                            if(goodsInfo.getWeight() > t1.getWeight())
                                return 1;
                            return -1;
                        }
                    });
                    recyclerView = findViewById(R.id.recyclerview);
                    adapter = new GoodsListRecyclerViewAdapter(StoreActivity.this, goodList);
                    recyclerView.setLayoutManager(new GridLayoutManager(StoreActivity.this,2));
                    recyclerView.setHasFixedSize(true);
                    adapter.setHeaderView(header);
                    recyclerView.setAdapter(adapter);
                }

            }
        };
        observable.compose(RxSchedulers.compose()).subscribe(mBaseObserver);
    }

    private static HashMap<String, String> getBaseMap() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uId", UserManager.INSTANCE.getUserId());
        params.put("accessToken", UserManager.INSTANCE.getAccessToken());
        return params;
    }
}
